<template>
    <view class="container">
        <view class="header">
            <status-bar></status-bar>
            <view class="user-info" @click="toUserInfo" v-if="Store.getters.isLogin">
                <view class="avatar-wrap">
                    <image class="avatar" :src="Store.state.user.info.info && Store.state.user.info.info.FHeadImg" mode="aspectFill" />
                </view>
                <view class="user-detail">
                    <view class="nickname">{{Store.state.user.info.info && Store.state.user.info.info.FUserName}}</view>
                    <view class="phone">{{Store.state.user.info.info && Store.state.user.info.info.FPhone}}</view>
                </view>
            </view>
            <view class="user-info" v-else @click="toLogin">
                <view class="avatar-wrap">
                    <image class="avatar" src="/static/default-avatar.png" mode="aspectFill" />
                </view>
                <view class="user-detail">
                    <view class="nickname">游客</view>
                    <view class="phone">未登录</view>
                </view>
            </view>
        </view>

        <view class="menu-list">
            <view class="menu-item" v-for="(item, index) in menuList" :key="index" @tap="handleMenuClick(item)">
                <view class="menu-item-left">
                    <!-- <text :class="['iconfont', item.icon]"></text> -->
                    <text class="menu-title">{{ item.title }}</text>
                </view>
                <!-- <text class="icon-right fs24"></text> -->
                <uni-icons type="right" size="20" color="#ccc"></uni-icons>

            </view>
        </view>

        <!-- <view class="version">版本号 v1.0.0</view> -->
    </view>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import dayjs from "dayjs";
import { onLoad, onShow, onPullDownRefresh } from "@dcloudio/uni-app";
import statusBar from "@/components/base/statusBar.vue";
import { globalLogin, getUserInfoFn, getLoginStatus } from '@/hooks/login';
import { useStore } from "vuex";
const Store = useStore();

const menuList = computed(() => {
    let userId = Store.state.user?.info?.info?.FUserId
    let list = [
        { title: '个人信息', icon: 'icon-user', path: '/pages/user/info' },
        { title: '实名认证', icon: 'icon-order', path: '/pages/user/realAuth' },
        { title: '紧急联系人', icon: 'icon-user', path: '/pages/user/relationList' },
        { title: '租赁订单', icon: 'icon-order', path: '/pages/order/list' },
        { title: '商户中心', icon: 'icon-order', path: `/pages/webview/webview?path=${encodeURIComponent(encodeURIComponent(`https://ok.ttjiandan.com/app/?hideNav=1&userId=${userId}&v=${new Date().getTime()}`))}` },
        // { title: '法大大签约中心', icon: 'icon-order', path: `/pages/webview/webview?path=${encodeURIComponent(encodeURIComponent('https://00002733.e.fadada.com/connect/login'))}` },
        // { title: '消息中心', icon: 'icon-message', path: '/pages/message/index' },
        // { title: '联系客服', icon: 'icon-service', path: '/pages/service/index' },
        // 用户协议不能登录后才能访问，会违规
        // { title: '用户协议', icon: 'icon-agreement', path: '/pages/agreement/index' }
    ]
    if(process.env.NODE_ENV === 'development') {
        list.push( { title: '商户中心local', icon: 'icon-order', path: `/pages/webview/webview?path=${encodeURIComponent(encodeURIComponent(`http://192.168.1.133:8092/app/?hideNav=1&userId=${userId}&v=${new Date().getTime()}`))}` })
    }
    if(process.env.NODE_ENV === 'development' || Store.state.user?.account?.mobile == '18950475114') {
        list.push( { title: '绑定主账号', icon: 'icon-order', path: `/pages/user/bindSub` }, )
    }
    return list
})

const handleMenuClick = (item: any) => {
    if(item.title.indexOf('商户中心') > -1  || item.title.indexOf('紧急联系人') > -1 && process.env.NODE_ENV === 'development') {
        uni.navigateTo({
            url: item.path
        })
        return
    }
    getLoginStatus(true).then(status => {
        if(status) {
            uni.navigateTo({
                url: item.path
            });
        }
    })
};

onPullDownRefresh(async () => {
    try {
        await getUserInfoFn();
        uni.stopPullDownRefresh();
    } catch (error) {
        uni.stopPullDownRefresh();
        console.log(error);
    }
})

onLoad(() => {


    getLoginStatus().then(state => {
        if(state) {
            getUserInfoFn();
        }
    })
});

onShow(() => {
    Store.commit('shop/SET_TEMP_STORE_NO', '');
});

const toUserInfo = () => {
    uni.navigateTo({
        url: '/pages/user/info'
    });
};

const toLogin = () => {
    globalLogin();
};
</script>

<style lang="scss">
.container {
    min-height: 100vh;
    background-color: #f8f8f8;

    .header {
        @include gradient(#dda62f, #e4c278, to bottom, 0%, 100%);
        padding-bottom: 30rpx;

        .user-info {
            display: flex;
            align-items: center;
            padding: 30rpx 40rpx;
            // #ifdef MP-ALIPAY
            margin-top: 80rpx;
            // #endif
            .avatar-wrap {
                width: 120rpx;
                height: 120rpx;
                border-radius: 60rpx;
                overflow: hidden;
                border: 4rpx solid rgba(255, 255, 255, 0.8);

                .avatar {
                    width: 100%;
                    height: 100%;
                }
            }

            .user-detail {
                margin-left: 30rpx;

                .nickname {
                    font-size: 36rpx;
                    color: #fff;
                    font-weight: bold;
                    margin-bottom: 10rpx;
                }

                .phone {
                    font-size: 28rpx;
                    color: rgba(255, 255, 255, 0.9);
                }
            }
        }
    }

    .menu-list {
        background-color: #fff;
        margin: 20rpx;
        border-radius: 16rpx;
        padding: 0 20rpx;

        .menu-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 100rpx;
            border-bottom: 1rpx solid #f5f5f5;

            &:last-child {
                border-bottom: none;
            }

            .menu-item-left {
                display: flex;
                align-items: center;

                .iconfont {
                    font-size: 40rpx;
                    color: #0278ff;
                    margin-right: 20rpx;
                }

                .menu-title {
                    font-size: 30rpx;
                    color: #333;
                }
            }
        }
    }

    .version {
        text-align: center;
        font-size: 24rpx;
        color: #999;
        margin-top: 60rpx;
    }
}
</style>
